﻿
#region Imports
using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eassets.Layered.Domain.Asset;
using Eassets.Service.Asset;
using System.Collections.ObjectModel;
#endregion


namespace Eassets.UI.Asset
{
    public partial class FixedAssetsCategory : System.Web.UI.Page
    {

        string checkAddorModify;

        /// <summary>
        /// Page load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                textBoxCategoryCodeAdd.Attributes.Add("OnChange", "javascript:this.value=toUpperCase(this.value);");
                LoadProgramTree();
                treeViewProgramTree.Enabled = true;
                DisableComponents();

            }
            UpdatePanelTreeView.Update();

        }

        /// <summary>
        /// Disable componants
        /// </summary>
        protected void DisableComponents()
        {
            textBoxCategoryCodeAdd.Enabled = false;
            textBoxFullCategoryCode.Enabled = false;
            textBoxDescriptionAdd.Enabled = false;
            textBoxMoreDetailsAdd.Enabled = false;
            CheckBoxIsAssetCodeUserDefine.Enabled = false;
        }

        /// <summary>
        /// Enable Componants
        /// </summary>
        protected void EnableComponents()
        {
            textBoxCategoryCodeAdd.Enabled = true;
            textBoxFullCategoryCode.Enabled = true;
            textBoxDescriptionAdd.Enabled = true;
            textBoxMoreDetailsAdd.Enabled = true;
            CheckBoxIsAssetCodeUserDefine.Enabled = false;
        }

        /// <summary>
        /// Clear Componants
        /// </summary>
        protected void ClearComponents()
        {
            //textBoxCategoryCodeAdd.Text = "";
            textBoxFullCategoryCode.Text = "";
            textBoxDescriptionAdd.Text = "";
            textBoxMoreDetailsAdd.Text = "";

        }

        /// <summary>
        /// Save Category
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void buttonSave_Click(object sender, EventArgs e)
        {
            Category category = new Category();
            CategoryService itemService = new CategoryService();
            if (Page.IsValid) // Validate the controls
            {
                if (ViewState["checkAddorModify"].ToString().Trim() == "Add")
                {
                    category.ParentId = Convert.ToInt32(ViewState["SelectedTreeNodeValue"]);
                    category.Code = textBoxCategoryCodeAdd.Text;
                    category.FullCategoryCode = textBoxFullCategoryCode.Text;
                    category.Description = textBoxDescriptionAdd.Text;
                    category.MoreDetails = textBoxMoreDetailsAdd.Text;
                    category.FullPath = "";

                    if (CheckBoxIsAssetCodeUserDefine.Checked)
                    {
                        category.IsUserDefined = true;
                    }
                    else
                    {
                        category.IsUserDefined = false;
                    }

                    try
                    {
                        itemService.AddCategory(category);
                        FlashMessage1.Message = "Saved Successfully";
                        FlashMessage1.CssClass = "okmessage";
                        FlashMessage1.Display();
                    }
                    catch (Exception ex)
                    {
                        FlashMessage1.Message = "Save Failed";
                        FlashMessage1.CssClass = "errormessage";
                        FlashMessage1.Display();
                        Response.Redirect("~/Error.aspx");
                    }
                    LoadProgramTree();
                    Response.Redirect("FixedAssetsCategory.aspx");
                }
                else if (ViewState["checkAddorModify"].ToString().Trim() == "Modify")
                {
                    category.ParentId = Convert.ToInt32(ViewState["CategoryId"]);
                    category.Code = textBoxCategoryCodeAdd.Text;
                    category.FullCategoryCode = textBoxFullCategoryCode.Text;
                    category.Description = textBoxDescriptionAdd.Text;
                    category.MoreDetails = textBoxMoreDetailsAdd.Text;
                    category.FullPath = "";

                    if (CheckBoxIsAssetCodeUserDefine.Checked)
                    {
                        category.IsUserDefined = true;
                    }
                    else
                    {
                        category.IsUserDefined = false;
                    }

                    try
                    {
                        itemService.UpdateCategory(category);
                    }
                    catch (Exception ex)
                    {
                        Response.Redirect("~/Error.aspx");
                    }
                    LoadProgramTree();
                    Response.Redirect("FixedAssetsCategory.aspx");
                }

            }
        }


        /// <summary>
        /// Prgram Tree Load
        /// </summary>
        private void LoadProgramTree()
        {
            treeViewProgramTree.Nodes.Clear();

            Collection<Category> AssetCategorys = new Collection<Category>();
            CategoryService AssetCategoryService = new CategoryService();


            AssetCategorys = AssetCategoryService.GetAllAssetCategory();//.GetErimsScreensByModuleId(Convert.ToInt64(dropDownListModule.SelectedValue));

            TreeNode newNode = new TreeNode("Asset Category", 0.ToString());
            treeViewProgramTree.Nodes.Add(newNode);
            FillProgramTree(AssetCategorys, newNode);

            treeViewProgramTree.CollapseAll();
            treeViewProgramTree.Nodes[0].Expand();

        }

        /// <summary>
        /// Fill Program Tree
        /// </summary>
        /// <param name="sourceCollection"></param>
        /// <param name="parentTreeNode"></param>
        private void FillProgramTree(Collection<Category> sourceCollection, TreeNode parentTreeNode)
        {
            // Get all children of the current parent
            Collection<Category> o = new Collection<Category>(
                sourceCollection.Where(
                    x =>
                    parentTreeNode.Value == 0.ToString()
                        ? (x.ParentId == null || x.ParentId == 0)
                        : (x.ParentId != null) && (x.ParentId.ToString() == parentTreeNode.Value)).ToList());

            foreach (Category orig in o)
            {
                TreeNode newNode = new TreeNode(orig.Description, orig.CategoryID.ToString());

                parentTreeNode.ChildNodes.Add(newNode);
                FillProgramTree(sourceCollection, newNode);
            }
        }

        /// <summary>
        /// Tree Node Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void treeViewProgramTreeNode_Click(object sender, EventArgs e)
        {
            hiddenFieldCategoryId.Value = ((TreeView)sender).SelectedValue;
            ViewState["SelectedTreeNodeValue"] = hiddenFieldCategoryId.Value;

            CategoryService categoryService = new CategoryService();

            Category category =
                categoryService.GetAssetCategoryById(Convert.ToInt64(hiddenFieldCategoryId.Value));

            if (category.ParentId != null)
            {
                hiddenFieldCategoryId.Value =
                    category.CategoryID.ToString();
                ViewState["CategoryId"] = hiddenFieldCategoryId.Value;

                textBoxCategoryCodeAdd.Text = category.Code;
                textBoxFullCategoryCode.Text = category.FullCategoryCode;
                textBoxMoreDetailsAdd.Text = category.MoreDetails;
                textBoxDescriptionAdd.Text = category.Description;

                if (category.IsUserDefined)
                {
                    CheckBoxIsAssetCodeUserDefine.Checked = Convert.ToBoolean(category.IsUserDefined);
                }
            }
            else
            {

            }

            DisableComponents();

        }

        protected void buttonSaveAdd_Click(object sender, EventArgs e)
        {

            ViewState["checkAddorModify"] = "Add";
            ClearComponents();
            EnableComponents();
            CheckBoxIsAssetCodeUserDefine.Checked = true;

        }

        protected void buttonModify_Click(object sender, EventArgs e)
        {
            ViewState["checkAddorModify"] = "Modify";
            EnableComponents();
        }

        protected void buttonDelete_Click(object sender, EventArgs e)
        {
            ViewState["checkAddorModify"] = "Delete";
            EnableComponents();

            CategoryService categoryService = new CategoryService();

            categoryService.DeleteCategory(Convert.ToInt64(ViewState["CategoryId"]));
        }

        protected void buttonClear_Click(object sender, EventArgs e)
        {
            ClearComponents();
        }
    }
}
